package com.tadpole.music.presenter.home;

import com.tadpole.music.bean.home.UserInfoBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.home.UserInfoIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取用户信息
 * Created By 孙岩
 * on 2020/9/18
 */
public class UserInfoPresenter extends BasePresenter<UserInfoIView> {
    @SuppressWarnings("unchecked")
    public void getUserInfo() {
        String url = UrlConfig.getUserInfoV2;
        String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
        DataModel.request(ModelToken.GET_HEAD_REQUEST)
                .token(token)
                .url(url)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if (obj.getInt("code") == 200) {
                                JSONObject object = new JSONObject(obj.getString("data"));
                                List<UserInfoBean.DataBean.CollegeBean> list = new ArrayList<>();
                                JSONArray array = new JSONArray(object.getString("college"));
                                for(int i=0;i<array.length();i++){
                                    JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                    UserInfoBean.DataBean.CollegeBean bean = new UserInfoBean.DataBean.CollegeBean(
                                            JsonParseUtil.getInt(jsonObject,"college_id")
                                    );
                                    list.add(bean);
                                }

                                UserInfoBean.DataBean bean = new UserInfoBean.DataBean(
                                        JsonParseUtil.getInt(object, "id"),
                                        JsonParseUtil.getBoolean(object, "bind_wxapp"),
                                        JsonParseUtil.getStr(object, "created_at"),
                                        JsonParseUtil.getStr(object, "updated_at"),
                                        JsonParseUtil.getStr(object, "nickname"),
                                        JsonParseUtil.getStr(object, "phone"),
                                        JsonParseUtil.getStr(object, "avatar"),
                                        JsonParseUtil.getInt(object, "gender"),
                                        JsonParseUtil.getStr(object, "birthday"),
                                        JsonParseUtil.getStr(object, "area"),
                                        JsonParseUtil.getStr(object, "role"),
                                        JsonParseUtil.getInt(object, "status"),
                                        JsonParseUtil.getStr(object, "login_ip"),
                                        JsonParseUtil.getStr(object, "login_at"),
                                        JsonParseUtil.getStr(object, "organization"),
                                        JsonParseUtil.getInt(object, "is_agent"),
                                        JsonParseUtil.getInt(object, "points"),
                                        list

                                );
                                getView().showUserInfo(bean);
                            }else if(obj.getInt("code")==401){
                                getView().noLogin();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showErr();
                    }

                    @Override
                    public void onComplete() {

                    }

                    @Override
                    public void onBefore() {

                    }
                });
    }
}
